package com.cn.kabalifeuser.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import rx.Subscription;

/**
 * Created by xiaolin on 2018/9/20.
 * function
 */

public abstract class BaseFragment extends Fragment {

    //RxBus
    protected Subscription rxBusSubscription;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(provideContentViewId(), container, false);
        initView(view);
        initRxBus();
        return view;
    }


    /**
     * 初始化页面布局
     *
     * @param view w
     */
    public void initView(View view) {
    }


    public void initRxBus() {

    }

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //取消RxBus订阅
        if (rxBusSubscription != null) {
            rxBusSubscription.unsubscribe();
            rxBusSubscription = null;
        }
    }


    /**
     * 跳转
     *
     * @param cls
     */
    protected void $startActivity(Class<?> cls) {
        $startActivity(cls, null);
    }

    /**
     * 跳转传参数
     *
     * @param cls
     * @param bundle
     */
    protected void $startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(getMe(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 使用Activity.this的地方统一使用getMe替代
     *
     * @return
     */
    public Context getMe() {
        return getActivity();
    }

}
